<?php
namespace app\jok\logic;
use app\common\model\systemAdmin;
use app\common\validate\SystemAdmin as SystemAdminValidate;
use app\common\model\systemRole;
class systemAdminLogic extends jokCommonLogic
{
    public function checkLogin($username,$pwd){
        if(!$username){
            return ['code'=>0,'msg'=>'账号不为空'];
        }
        if(!$pwd){
            return ['code'=>0,'msg'=>'密码不为空'];
        }
        $find = systemAdmin::get(['username|mobile'=>$username]);
        if($find){
            $checkpwd = encrypwd($pwd,$find['salt']);
            if($find['status'] != 1){
                return ['code'=>0,'msg'=>'已禁用，请联系管理员！'];
            }else if($checkpwd == $find['password']){
                $arr = $find;
                unset($arr['salt']);
                unset($arr['password']);
                unset($arr['roles']);
                $result = $this->setToken($find);
                if($result){
                    return ['code'=>1,'msg'=>'登陆成功','token'=>$result, 'userinfo'=>$arr];
                }else{
                    return ['code'=>0,'msg'=>'设置token错误'];
                }
            }else{
                return ['code'=>0,'msg'=>'密码不正确'];
            }
        }else{
            return ['code'=>0,'msg'=>'用户不存在'];
        }
    }
    public function adminGetList($param){
        $fun = function($query)use($param){
            $map = [];
            if(!empty($param['key'])){
                $map['username|nickname|mobile'] = $param['key'];
            }
            if(!empty($param['status'])){
                $map['status'] = $param['status'] - 1;
            }
            $query->where($map);
        };
        $list = (new systemAdmin)
            ->where($fun)
            ->order('status desc')
            ->paginate(['page'=>$param['page'] ?? 1,'list_rows'=>$param['pageSize'] ?? 10])
            ->toArray();
        return ['code'=>1,'msg'=>'获取成功','data'=>$list];
    }
    public function adminAddEdit($param){
        $check = new SystemAdminValidate;
        $systemAdmin = new systemAdmin;
        if(isset($param['id'])){
            if(true !== $check->scene('edit')->check($param)){
                return ['code'=>0,'msg'=>$check->getError()];
            }
            if($param['id'] == 1){
                return ['code'=>0,'msg'=>'此账号不可修改'];
            }
            if(isset($param['password'])){
                $param['password'] = encrypwd($param['password'],$param['salt']);
            }
            $result = $systemAdmin->allowField(true)->isUpdate(true)->save($param);
            $title = '编辑';
        }else{
            if(true !== $check->scene('add')->check($param)){
                return ['code'=>0,'msg'=>$check->getError()];
            }
            $param['salt'] = rand(1111,9999);
            $param['password'] = encrypwd($param['password'],$param['salt']);
            unset($param['id']);
            $result = $systemAdmin->allowField(true)->save($param);
            $title = '新增';
        }
        if($result){
            return ['code'=>1,'msg'=>$title.'成功'];
        }else{
            return ['code'=>0,'msg'=>$title.'失败'];
        }

    }
    public function adminDataDetail($param){
        if(empty($param['id'])){
            return ['code'=>0,'msg'=>'参数错误'];
        }
        $fun = function($query)use($param){
            $query->field('id,username,nickname,salt,mobile,roles')->where(['id'=>$param['id']]);
        };
        $result = systemAdmin::get($fun);
        return ['code'=>1,'msg'=>'获取成功','data'=>$result];
    }
    
    
    public function roleGetList($param){
        $fun = function($query)use($param){
            $map = [];
            if(!empty($param['key'])){
                $map['name'] = $param['key'];
            }
            $query->where($map);
        };
        $list = (new systemRole)
            ->where($fun)
            ->paginate(['page'=>$param['page'],'list_rows'=>$param['pageSize']])
            ->toArray();
        return ['code'=>1,'msg'=>'获取成功','list'=>$list];
    }
    public function roleAddEdit($param){
        $systemRole = new systemRole;
        if(!empty($param['id'])){
            $result = $systemRole->allowField(true)->isUpdate(true)->save($param);
            $title = '编辑';
        }else{
            unset($param['id']);
            $result = $systemRole->allowField(true)->save($param);
            $title = '新增';
        }
        if($result){
            return ['code'=>1,'msg'=>$title.'成功'];
        }else{
            return ['code'=>0,'msg'=>$title.'失败'];
        }

    }
    public function roleDataDetail($param){
        if(empty($param['id'])){
            return ['code'=>0,'msg'=>'参数错误'];
        }
        $fun = function($query)use($param){
            $query->field('id,name')->where(['id'=>$param['id']]);
        };
        $result = systemRole::get($fun);
        return ['code'=>1,'msg'=>'获取成功','data'=>$result];
    }
}